package org.dallasrowing.logbook.ui.view;

import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class SWTReportsWindow implements ReportsWindow {

	private static final int MIN_YEAR = 2009;
	private Shell reportDialog;
	private Combo reportsCombo;
	private Button showReport;
//	private Button exportButton;
	private Combo yearCombo;

	public SWTReportsWindow(Shell parent) {
		createWindow(parent);
	}

	public void show() {
		reportDialog.open(); 
	}

	private void createWindow(Shell parent) {
		reportDialog = new Shell (parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE );
		reportDialog.setText ("Show Report...");
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.marginHeight=10;
		gridLayout.marginWidth=10;
		reportDialog.setLayout(gridLayout);
		
		createWidgets();
		
		reportDialog.setMinimumSize(300, 100);
		reportDialog.pack();
	}

	private void createWidgets() {
		Label l = new Label(reportDialog, SWT.None);
		l.setText("Available Reports:");
		l.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));
		
		reportsCombo = new Combo(reportDialog, SWT.DROP_DOWN | SWT.READ_ONLY );
		reportsCombo.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
		
		l = new Label(reportDialog, SWT.NONE);
		l.setText("For year:");
		l.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));
		
		yearCombo = new Combo(reportDialog, SWT.DROP_DOWN | SWT.READ_ONLY );
		fillWithYears();
		yearCombo.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
		
		
		showReport = new Button(reportDialog, SWT.NONE);
		showReport.setText("Generate Report");
		showReport.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, false, 2, 1));
		
//		reportCanvas = new Canvas( reportDialog, SWT.BORDER | SWT.V_SCROLL |SWT.H_SCROLL );
//		reportCanvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
//		
//		exportButton = new Button( reportDialog, SWT.NONE);
//		exportButton.setText("Export to PDF...");
//		exportButton.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, false, 2, 1));
	}

	private void fillWithYears() {
		int currentYear = Calendar.getInstance().get(Calendar.YEAR);
		for (int year = MIN_YEAR; year <= currentYear ; year++ )
		{
			yearCombo.add(""+year);
		}
		yearCombo.select(yearCombo.getItemCount()-1);
	}

	public void setGenerateReportListener(final Runnable runnable) {
		showReport.addListener(SWT.Selection, new Listener() {
		
			public void handleEvent(Event arg0) {
				runnable.run();
			}
		});
	}

	public void addReport(String report) {
		reportsCombo.add(report);
	}

	public int getSelectedReport() {
		return reportsCombo.getSelectionIndex();
	}

	public void hide() {
		reportDialog.dispose();
	}

	public int getSelectedYear() {
		return Integer.valueOf(yearCombo.getItem(yearCombo.getSelectionIndex()));
	}

}
